package persistent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

import com.db4o.*;
import com.db4o.config.Configuration;
import com.db4o.cs.Db4oClientServer;
import com.db4o.cs.config.ServerConfiguration;
import com.db4o.reflect.jdk.JdkReflector;

public class Db4oServletListener implements ServletContextListener,
		ServletRequestListener {
	// keys for values in WEB-INF/web.xml
	
	/**
	 * Port the server is listening at
	 */
	public static final int PORT = 7777;
	public static final String KEY_DB4O_FILE_NAME = "KEY_DB4O_FILE_NAME";
	public static final String KEY_DB4O_SERVER = "KEY_DB4O_SERVER";
	public static final String KEY_DB4O_SESSION = "KEY_DB4O_SESSION";
	
	

	@Override
	public void requestDestroyed(ServletRequestEvent requestEvent) {
	    ObjectContainer session = (ObjectContainer) requestEvent
	            .getServletRequest().getAttribute(KEY_DB4O_SESSION);

	    close(session);
	}

	@Override
	public void requestInitialized(ServletRequestEvent requestEvent) {
	    ObjectServer rootContainer = (ObjectServer) requestEvent
	            .getServletContext().getAttribute(Db4oServletListener.KEY_DB4O_SERVER);

	    ObjectContainer session = rootContainer.openClient();
	    requestEvent.getServletRequest().setAttribute(KEY_DB4O_SESSION, session);
	}


	@Override
	public void contextDestroyed(ServletContextEvent event) {
	    ServletContext context = event.getServletContext();
	    ObjectServer rootContainer = (ObjectServer) context.getAttribute(KEY_DB4O_SERVER);
	    context.removeAttribute(KEY_DB4O_SERVER);
	    close(rootContainer);
	    context.log("db4o shutdown");
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
	    ServletContext context = event.getServletContext();
	    String filePath = context.getRealPath("WEB-INF/"
	            + context.getInitParameter(KEY_DB4O_FILE_NAME));

        ServerConfiguration config = Db4oClientServer.newServerConfiguration();
        config.common().reflectWith(new JdkReflector(getClass().getClassLoader()));
        
	    ObjectServer rootContainer = Db4oClientServer.openServer(config,filePath, 0);
	    context.setAttribute(KEY_DB4O_SERVER, rootContainer);
	    context.log("db4o startup on " + filePath);
	    
	    initialAuth(rootContainer);
	}
	
	private void initialAuth(ObjectServer db) {
		ObjectContainer container = db.openClient();
		container.store(new User("admin","admin"));
		container.commit();
		container.close();
		
	}
	
	private void close(ObjectServer db) {
		if(db != null) db.close();
	}

	private void close(ObjectContainer db) {
		if(db != null) db.close();
	}

}
